<?php

namespace App\Api;

use PhalApi\Api;


/**
 * 身份证格式化
 *
 * @author: dogstar <chanzonghuang@gmail.com> 2014-10-04
 */
class Idcard extends Api
{

    public function getRules()
    {
        return array(
            'idcard' => array(
                'name' => array( 'name' => 'name', 'require' => false,  'desc' => 'name' ),
                'number' => array( 'name' => 'number', 'require' => false, 'desc' => 'number' ),
            ),
        );
    }

    /**
     * rok-token
     * @desc 多条件查询接口
     * @return array data 被查数据
     * @exception 400 非法请求，参数传递错误
     */
    public function idcard()
    {
        header('Access-Control-Allow-Origin:*');

        $sheng = array();
        $sheng[11]="北京";
        $sheng[12]="天津";
        $sheng[13]="河北";
        $sheng[14]="山西";
        $sheng[15]="内蒙古";
        $sheng[21]="辽宁";
        $sheng[22]="吉林";
        $sheng[23]="黑龙江";
        $sheng[31]="上海";
        $sheng[32]="江苏";
        $sheng[33]="浙江";
        $sheng[34]="安徽";
        $sheng[35]="福建";
        $sheng[36]="江西";
        $sheng[37]="山东";
        $sheng[41]="河南";
        $sheng[42]="湖北";
        $sheng[43]="湖南";
        $sheng[44]="广东";
        $sheng[45]="广西";
        $sheng[46]="海南";
        $sheng[50]="重庆";
        $sheng[51]="四川";
        $sheng[52]="贵州";
        $sheng[53]="云南";
        $sheng[54]="西藏";
        $sheng[61]="陕西";
        $sheng[62]="甘肃";
        $sheng[63]="青海";
        $sheng[64]="宁夏";
        $sheng[65]="新疆";


        $info = \PhalApi\DI()->notorm
            ->rok_card
            ->where('used', 'no')
            ->fetchOne();


//        如果有身份证
        if (  $info  ){

            $title = substr($info['number'],0,6);

            $address = \PhalApi\DI()->notorm
                ->idcard_info_list
                ->where('id_header', $title)
                ->fetchOne();

            $Shen = $sheng[substr($info['number'],0,2)];
            $info_address = $address['info'];
            $birthday = substr($info['number'],6,8);
            $year = substr($birthday,0,4);
            $moon = substr($birthday,4,2);
            $day = substr($birthday,6,2);
            $birthday = $year.'-'.$moon.'-'.$day;
            $sex = substr($info['number'],16,1)%2;

            $update = array(
                'number'=>$info['number'],
                'sheng' => $Shen,
                'info'=>$info_address,
                'birthday'=>$birthday,
                'year'=>$year,
                'moon'=>$moon,
                'day'=>$day,
                'sex'=> $sex,
                'used' => 'yes',
                "time" => date("Y-m-d H:i:s",time() ),
                "addtime" => date("Y-m-d H:i:s",time() ),
            );


            $all = \PhalApi\DI()->notorm
                ->rok_card
                ->where('id',$info['id'])
                ->update($update);

            return array(
                $update,
                $all,
                $info['id']
            );
        }


    }


}
